package gatperdut.chato.core.messaging.packagers;

import gatperdut.chato.constants.CSConstantsMovement.Direction;
import gatperdut.chato.core.room.RoomController;

import java.util.Arrays;

import org.json.simple.JSONObject;

// TODO use the Room object, not the controller.
public class RoomsPackager extends GenericPackager<RoomController> {

    public static enum RoomJSONAttr {
        ID("id"), TITLE("title"), DESCRIPTION("description"), NORTH("n"), EAST("e"), SOUTH("s"), WEST("w"), UP("u"), DOWN("d");

        private final String name;

        private RoomJSONAttr( String name ) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    };

    @SuppressWarnings( "unchecked" )
    @Override
    public JSONObject packageOne( RoomController roomController ) {
        JSONObject roomJO = new JSONObject();

        roomJO.put( RoomJSONAttr.ID.getName(), roomController.getRoom().cdoID().toURIFragment() );
        roomJO.put( RoomJSONAttr.TITLE.getName(), roomController.getRoom().getTitle() );
        roomJO.put( RoomJSONAttr.DESCRIPTION.getName(), roomController.getRoom().getDescription() );
        roomJO.put( RoomJSONAttr.NORTH.getName(), roomController.getExitsController().getAdjacentRoomCDOID( Direction.NORTH ) );
        roomJO.put( RoomJSONAttr.EAST.getName(), roomController.getExitsController().getAdjacentRoomCDOID( Direction.EAST ) );
        roomJO.put( RoomJSONAttr.SOUTH.getName(), roomController.getExitsController().getAdjacentRoomCDOID( Direction.SOUTH ) );
        roomJO.put( RoomJSONAttr.WEST.getName(), roomController.getExitsController().getAdjacentRoomCDOID( Direction.WEST ) );
        roomJO.put( RoomJSONAttr.UP.getName(), roomController.getExitsController().getAdjacentRoomCDOID( Direction.UP ) );
        roomJO.put( RoomJSONAttr.DOWN.getName(), roomController.getExitsController().getAdjacentRoomCDOID( Direction.DOWN ) );

        return roomJO;
    }

    @Override
    public RoomController[] toArray( Object[] roomsArray ) {
        return Arrays.copyOf( roomsArray, roomsArray.length, RoomController[].class );
    }
}
